﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using websoniBO;

namespace websoni.Adminpages
{
    public partial class WebForm5 : System.Web.UI.Page
    {
        Poems p = new Poems();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //tbnewtitle.Visible = false;
                btnupdate.Visible = false;
                tbpid.Visible = false;
                DataTable dt = p.GetRecords(-99, "", "", false, " and ");
                GridView1.DataSource = dt;
                GridView1.DataBind();
            }

        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {

        }

        protected void btndelete_Command(object sender, CommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                DataTable dt1 = p.GetRecords(Convert.ToInt64(e.CommandArgument), "", "", false, " and ");
                p.pid = Convert.ToInt64(dt1.Rows[0]["pid"].ToString());
                p.title = dt1.Rows[0]["title"].ToString();
                p.poem = dt1.Rows[0]["poem"].ToString();
                p.isdeleted = true;
                p.SaveRecords();
                Response.Redirect("~/Adminpages/APoem.aspx");
            }
        }

        protected void btnok_Click(object sender, EventArgs e)
        {
            p.pid = -99;
            p.title = tbtitle.Text + "</br>" +  DateTime.Now.ToString();
            p.poem = tbpoem.Text.Replace(Environment.NewLine,"</br>");
            p.isdeleted = false;
            p.SaveRecords();
            Response.Redirect("~/Adminpages/APoem.aspx");
        }

        protected void btnedit_Command(object sender, CommandEventArgs e)
        {
            btnupdate.Visible = true; tbtitle.ReadOnly = true; btnok.Visible = false;
            DataTable dte = p.GetRecords(Convert.ToInt64(e.CommandArgument), "", "", false, " and ");
            tbpid.Text = dte.Rows[0]["pid"].ToString();
            tbtitle.Text = dte.Rows[0]["title"].ToString().Replace("</br>"," ");
            //tbnewtitle.Text = dte.Rows[0]["title"].ToString().Replace("</br>", " ");
            tbpoem.Text = dte.Rows[0]["poem"].ToString().Replace("</br>",Environment.NewLine);

        }

        protected void btnupdate_Click(object sender, EventArgs e)
        {
            p.pid = Convert.ToInt64(tbpid.Text);
            //if (tbtitle.Text!=tbnewtitle.Text)
            //{
            //    p.title = tbtitle.Text + "</br>" + DateTime.Now.ToString();
            //}
            //else
                p.title = tbtitle.Text;
            p.poem = tbpoem.Text.Replace(Environment.NewLine, "</br>");
            p.isdeleted = false;
            p.SaveRecords();
            Response.Redirect("~/Adminpages/APoem.aspx");
        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {

        }

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {

        }

       

        protected void tbtitle_TextChanged1(object sender, EventArgs e)
        {

        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex= e.NewPageIndex;
            DataTable dt1 = p.GetRecords(-99, "", "", false, " and ");
            GridView1.DataSource = dt1;
            GridView1.DataBind();
        }
    }
}
